﻿using System;
using System.Collections.Generic;
using System.Text;
using Ecreate.Data;
using Ecreate.Model;
using System.Collections;

namespace Ecreate.Business
{
    public class ProductClassService
    {
        public bool Insert (Ecreate.Model.ProductClass p)
        {
            try
            {
                string id = this.getID(p);
                p.Classid = id;
                if (p.Parentclassid == "-1")
                {
                    p.Flevel = 0;
                }
                else
                {
                    p.Flevel = 1;
                }
                ProductClass searchp = FactoryDBEngine.getIBatisService().getIMapper().QueryForObject<ProductClass>("t_productclass_GetByName", p.Classname);
                if (searchp!=null)
                {
                    return false;
                }
                object o = FactoryDBEngine.getIBatisService().Insert("t_productclass_insert", p);
                return true;
            }
            catch
            {

                return false;
            }
        }
        public void Delete (Ecreate.Model.ProductClass p)
        {

            object o = FactoryDBEngine.getIBatisService().Delete("t_productclass_deletebyclassid", p);

        }


        public void Update(Ecreate.Model.ProductClass p)
        {

            object o = FactoryDBEngine.getIBatisService().Update("t_product_class_Update", p);


        }



        public string getID(Ecreate.Model.ProductClass p)
        {
            Hashtable map = new Hashtable();
            map.Add("parentid", p.Parentclassid);
            FactoryDBEngine.getIBatisService().Insert("t_product_class_getmaxclassid", map);
            string value = map["ret"].ToString();
            return value;
        }

        public ProductClass getProductClassByClassID(string classid)
        {

            ProductClass model=(ProductClass)FactoryDBEngine.getIBatisService().QueryForObject("t_productclass_GetByClassid", classid);
            return model;

        }

        public IList getProductClassByParentID(string parentclassid)
        {

            IList l = FactoryDBEngine.getIBatisService().ExecuteQueryForList("t_productclass_GetByParentid", parentclassid);
            return l;

        }


        public string getXmlString()
        {
            ArrayList l = (ArrayList)FactoryDBEngine.getIBatisService().ExecuteQueryForList("t_productclass_SelectAll", null);
            ArrayList l_data = (ArrayList)l.Clone();
            StringBuilder xml = new StringBuilder();
            xml.Append("<?xml version='1.0' encoding='gb2312'?>");
            xml.Append("<tree id='0'>");
            foreach (ProductClass p in l)
            {
                if (p.Parentclassid == "-1")
                {
                    beginItem(p, xml);
                    getChildXml(p, xml, l_data);
                    endItem(xml);
                }
            }
            xml.Append("</tree>");
            return xml.ToString();
        }
        private void getChildXml(ProductClass p, StringBuilder xml, ArrayList data)
        {
            foreach (ProductClass pitem in data)
            {
                if (pitem.Parentclassid == p.Classid)
                {
                    beginItem(pitem, xml);
                    getChildXml(pitem, xml, data);
                    endItem(xml);
                }
            }
        }
        private void beginItem(ProductClass p, StringBuilder xml)
        {
            string img = "";
            string state = "";
            if (p.Parentclassid == "-1")
            {
                //img = "im0='menu_root.gif' im1='menu_root.gif' im2='menu_root.gif' ";
                state = " select='1' open='1' ";
            }
            else
            {
                //img = "im0='menu_leaf.gif' im1='menu_leaf.gif' im2='menu_leaf.gif' ";
            }
            xml.Append((new StringBuilder("<item id='")).Append(p.Classid).Append("' text='").Append(p.Classname).Append("' ").Append(img).Append(state).Append(">"));
            xml.Append("<userdata name='level'>");
            xml.Append(p.Flevel.ToString());
            xml.Append("</userdata>");
        }
        private void endItem(StringBuilder xml)
        {
            xml.Append("</item>");
        }
    }
}
